package com.heo.util;

import org.springframework.util.StringUtils;

public class ByteHelper {
	/**
	 * 填充字节数组，将某个字节数组前面填充其他的字节。如果长度不够，则不填充
	 * 
	 * @param srcByteArray
	 * @param fillByte
	 * @param fillLength
	 * @return
	 */
	public static byte[] fillByteArray(byte[] srcByteArray, byte fillByte, int fillLength) {
		if (srcByteArray == null || srcByteArray.length >= fillLength) {
			return srcByteArray;
		}
		byte[] resultArray = new byte[fillLength];
		int index = 0;
		for (int i = 0; i < fillLength; i++) {
			if (i < fillLength - srcByteArray.length) {
				resultArray[i] = fillByte;
			} else {
				resultArray[i] = srcByteArray[index];
				index++;
			}
		}
		return resultArray;
	}

	/**
	 * @Title:hexString2Bytes
	 * @Description:16进制字符串转字节数组，传进来的格式是0A0B0C1F，这样连着的字节，也可以是 0A 0B 0C 1F每个字节有空格
	 * @return 字节数组
	 * @param src
	 *            16进制字符串
	 * @return
	 */
	public static byte[] hexStringToBytes(String src) {
		if (StringUtils.isEmpty(src)) {
			return null;
		}
		if (src.indexOf(" ") != -1) {
			src = src.replace(" ", "");
		}
		int l = src.length() / 2;
		byte[] ret = new byte[l];
		for (int i = 0; i < l; i++) {
			ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
		}
		return ret;
	}

	// public static byte[] strToByteArray(String commandStr)
	// {
	// try
	// {
	// if (commandStr == null)
	// {
	// return null;
	// }
	// String[] strArray = commandStr.split(",");
	// byte[] result = new byte[strArray.length];
	// for (int i = 0; i < strArray.length; i++)
	// {
	// result[i] = Byte.parseByte(strArray[i]);
	// }
	// return result;
	// }
	// catch (Exception e)
	// {
	// e.printStackTrace();
	// return null;
	// }
	// }

	public static Byte strToByte(String commandStr) {
		try {
			if (commandStr == null) {
				return null;
			}
			return Byte.parseByte(commandStr);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 将字符串转换成前面是二进制0的字节数组
	 * 
	 * @param str
	 * @param arrayLength
	 * @return
	 */
	public static byte[] stringToByteArray(String str, int arrayLength) {
		if (str == null || str.length() > arrayLength) {
			return null;
		}
		byte[] newArray = new byte[arrayLength - str.getBytes().length];
		for (int i = 0; i < newArray.length; i++) {
			newArray[i] = (byte) 0x00;
		}
		return margeByte(newArray, str.getBytes(), null, null, null, null, null, null, null, null);
	}

	/**
	 * 将字节转换为string
	 * 
	 * @param bt
	 * @return
	 */
	public static String byteToHexString(byte[] bt) {
		if (bt == null || bt.length == 0) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < bt.length; i++) {
			int tmp = bt[i] & 0xff;
			String tmpStr = Integer.toHexString(tmp);
			if (tmpStr.length() < 2)
				sb.append("0");
			sb.append(tmpStr + " ");
		}
		return sb.toString().substring(0, sb.toString().length() - 1).toUpperCase();
	}

	/**
	 * 将字节转化成short整数
	 * 
	 * @param b:字节数组
	 * @param startIndex:开始转换位置
	 * @param length:要转换的字节数
	 * @return 转换后结果
	 */
	public static short byteToShort(byte[] b, int startIndex, int length) {
		short iRet = 0;
		short iTep;
		for (int i = startIndex; i < startIndex + length && i < b.length; i++) {
			iTep = b[i];
			if (iTep < 0) {
				iTep += 256;
			}
			// iTep = iTep << (iLen-1-(i-iFrom)*8);
			// iRet = iRet | iTep;
			iRet = (short) ((iRet << 8) | iTep);
		}
		return iRet;
	}

	/**
	 * 取字节数组中的子数组
	 * 
	 * @param b
	 * @param startIndex
	 * @param length
	 * @return
	 */
	public static byte[] toSubByteArray(byte[] b, int startIndex, int length) {
		byte[] result = new byte[length];
		int j = 0;
		for (int i = startIndex; i < startIndex + length && i < b.length; i++) {
			result[j] = b[i];
			j++;
		}
		return result;
	}

	/**
	 * 将字节转化成整数
	 * 
	 * @param b:字节数组
	 * @param startIndex:开始转换位置
	 * @param length:要转换的字节数
	 * @return 转换后结果
	 */
	public static int byteToInt(byte[] b, int startIndex, int length) {
		int iRet = 0;
		int iTep;
		for (int i = startIndex; i < startIndex + length && i < b.length; i++) {
			iTep = b[i];
			if (iTep < 0) {
				iTep += 256;
			}
			// iTep = iTep << (iLen-1-(i-iFrom)*8);
			// iRet = iRet | iTep;
			iRet = (iRet << 8) | iTep;
		}
		return iRet;
	}

	/**
	 * 将字节转化成字符串
	 * 
	 * @param b:转换后存放结果的字节数组
	 * @param startIndex:存放字节起始位置
	 * @param length:将字符串转换成的字节数
	 * @param bCut:字符串的头部要切除的字符
	 */
	public static String byteToString(byte[] b, int startIndex, int length, byte bCut) {
		if (b == null || startIndex + length > b.length) {
			return null;
		}
		if (length < 1) {
			return "";
		}

		String str = new String(b, startIndex, length);
		if (str == null || str.length() == 0) {
			return "";
		}
		int i = 0;
		while (str.charAt(i) == (char) bCut) {
			i++;
		}
		return str.substring(i);
	}

	/**
	 * 把多个字节数组合并成一个
	 * 
	 * @param b1
	 * @param b2
	 * @return
	 */
	public static byte[] margeByte(byte[] b0, byte[] b1, byte[] b2, byte[] b3, byte[] b4, byte[] b5, byte[] b6,
			byte[] b7, byte[] b8, byte[] b9) {
		b0 = (b0 == null) ? new byte[0] : b0;
		b1 = (b1 == null) ? new byte[0] : b1;
		b2 = (b2 == null) ? new byte[0] : b2;
		b3 = (b3 == null) ? new byte[0] : b3;
		b4 = (b4 == null) ? new byte[0] : b4;
		b5 = (b5 == null) ? new byte[0] : b5;
		b6 = (b6 == null) ? new byte[0] : b6;
		b7 = (b7 == null) ? new byte[0] : b7;
		b8 = (b8 == null) ? new byte[0] : b8;
		b9 = (b9 == null) ? new byte[0] : b9;

		byte[] result = new byte[b0.length + b1.length + b2.length + b3.length + b4.length + b5.length + b6.length
				+ b7.length + b8.length + b9.length];
		int index = 0;
		for (int i = 0; i < b0.length; i++) {
			result[index] = b0[i];
			index++;
		}
		for (int i = 0; i < b1.length; i++) {
			result[index] = b1[i];
			index++;
		}
		for (int i = 0; i < b2.length; i++) {
			result[index] = b2[i];
			index++;
		}
		for (int i = 0; i < b3.length; i++) {
			result[index] = b3[i];
			index++;
		}
		for (int i = 0; i < b4.length; i++) {
			result[index] = b4[i];
			index++;
		}
		for (int i = 0; i < b5.length; i++) {
			result[index] = b5[i];
			index++;
		}
		for (int i = 0; i < b6.length; i++) {
			result[index] = b6[i];
			index++;
		}
		for (int i = 0; i < b7.length; i++) {
			result[index] = b7[i];
			index++;
		}
		for (int i = 0; i < b8.length; i++) {
			result[index] = b8[i];
			index++;
		}
		for (int i = 0; i < b9.length; i++) {
			result[index] = b9[i];
			index++;
		}
		return result;
	}

	/*
	 * 将int转为低字节在后，高字节在前的byte数组 b[0] = 11111111(0xff) & 01100001 b[1] =
	 * 11111111(0xff) & 00000000 b[2] = 11111111(0xff) & 00000000 b[3] =
	 * 11111111(0xff) & 00000000
	 */
	public static byte[] intToByteArray(int value) {
		byte[] src = new byte[4];
		src[0] = (byte) ((value >> 24) & 0xFF);
		src[1] = (byte) ((value >> 16) & 0xFF);
		src[2] = (byte) ((value >> 8) & 0xFF);
		src[3] = (byte) (value & 0xFF);
		return src;
	}

	public static byte intToByte(int value) {
		return (byte) value;
	}

	/*
	 * 将int转为低字节在后，高字节在前的byte数组 b[0] = 11111111(0xff) & 01100001 b[1] =
	 * 11111111(0xff) & 00000000 b[2] = 11111111(0xff) & 00000000 b[3] =
	 * 11111111(0xff) & 00000000
	 */
	public static byte[] shortToByteArray(short value) {
		byte[] src = new byte[2];
		src[0] = (byte) ((value >> 8) & 0xFF);
		src[1] = (byte) (value & 0xFF);
		return src;
	}

	/*
	 * 将int转为低字节在后，高字节在前的byte数组 b[0] = 11111111(0xff) & 01100001 b[1] =
	 * 11111111(0xff) & 00000000 b[2] = 11111111(0xff) & 00000000 b[3] =
	 * 11111111(0xff) & 00000000
	 */
	public static byte[] longToByteArray(long value) {
		byte[] result = new byte[8];
		result[0] = (byte) (value >>> 56);// 取最高8位放到0下标
		result[1] = (byte) (value >>> 48);// 取最高8位放到0下标
		result[2] = (byte) (value >>> 40);// 取最高8位放到0下标
		result[3] = (byte) (value >>> 32);// 取最高8位放到0下标
		result[4] = (byte) (value >>> 24);// 取最高8位放到0下标
		result[5] = (byte) (value >>> 16);// 取次高8为放到1下标
		result[6] = (byte) (value >>> 8); // 取次低8位放到2下标
		result[7] = (byte) (value); // 取最低8位放到3下标
		return result;
	}

	/**
	 * 把int转换为16进制字符串
	 * 
	 * @param value
	 * @return
	 */
	public static String intToHexString(int value) {
		return byteToHexString(intToByteArray(value));
	}

	/**
	 * 把short转换为16进制字符串
	 * 
	 * @param value
	 * @return
	 */
	public static String shortToHexString(short value) {
		return byteToHexString(shortToByteArray(value));
	}

	/**
	 * 把long转换为16进制字符串
	 * 
	 * @param value
	 * @return
	 */
	public static String longToHexString(long value) {
		return byteToHexString(longToByteArray(value));
	}

	public static int hex16To10(String code) {
		return Integer.parseInt(code, 16);
	}
	
	public static String hex16To10(String code, int length) {
		StringBuilder codeStart = new StringBuilder(code.substring(0, 2));
		String codes = code.substring(2);
		int codeInt = Integer.parseInt(codes, 16);
		int size = length - String.valueOf(codeInt).length();
		for (int i = 0; i < size; i++) {
			codeStart.append("0");
		}
		codeStart.append(codeInt);
		return codeStart.toString();
	}
	
	/**
	 * String的字符串转换成unicode的String
	 * 
	 * @param String
	 *            strText 全角字符串
	 * @return String 每个unicode之间无分隔符
	 * @throws Exception
	 */
	public static String hexIntegerToBytes(int code, int length) {
		String str = "";
		if (code > 65535) {
			return "0000";
		}
		str = Integer.toHexString(code);
		int len = str.length();
		if (len == length) {
			return str;
		}

		for (int i = 0; i < length - len; i++) {
			str = "0" + str;
		}
		return str.toUpperCase();
	}

	
	public static String hex10To16(int hex, int length) {
		String hexs = Integer.toHexString(hex);
		StringBuilder codeStart = new StringBuilder();
		int size = length - String.valueOf(hexs).length();
		for(int i = 0; i < size; i++){
			codeStart.append("0");
		}
		codeStart.append(hexs);
		return codeStart.toString();
	}
	
	public static void main(String[] args) {
		byte[] src = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };

		byte[] xx = fillByteArray(src, (byte) 0x00, 10);
		for (int i = 0; i < xx.length; i++) {
			System.out.println(xx[i]);
		}
	}
}
